package com.xiaoyan.texteditor.service;

import java.awt.*;
import java.awt.datatransfer.*;
import java.io.IOException;

/**
 * ClassName: EditorServiceImpl
 * Package: com.xiaoyan.texteditor.service
 * Description:
 *
 * @Author Xiaoyan_Lu
 * @Create 2023/12/4 16:12
 * @Version 1.0
 */
public class EditorServiceImpl implements EditorService{
    @Override
    public void copy(String data) {
        //获取剪切板
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        //转换
        Transferable trans = new StringSelection(data);
        //传输到剪切板
        clipboard.setContents(trans,null);
    }

    @Override
    public String paste() {
        //获取剪切板
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        //获取内容
        Transferable contents = clipboard.getContents(null);
        if(contents == null){
            return "";
        }else {
            try {
                String transferData = (String)contents.getTransferData(DataFlavor.stringFlavor);
                return transferData;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    public void shear(String data) {
        this.copy(data);
    }
}
